package com.dao.impl;

import com.dao.EarthDao;
import com.web.common.exception.DataAccessException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.web.common.utils.SslUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

/**
 * @Author wangzhipeng
 * @Date 2020-08-31 12:00 下午
 * @Description
 */
public class EarthDaoImpl implements EarthDao {

    @Override
    public String getEarthquakeInfo() throws Exception {
        String url = "http://data.earthquake.cn/datashare/report.shtml?PAGEID=zxdzall";
        URL url1 = new URL(url);
        SslUtils.ignoreSsl();
        Document document = Jsoup.connect(url).get();



        Elements table = document.getElementsByClass("cls-data-table");
        JSONArray result = new JSONArray();
        if (table.size() != 0) {
            Elements data = table.get(0).getElementsByClass("cls-data-content-list");
            for (int i = 0; i < data.size() / 7; i++) {
                JSONObject single = new JSONObject();
                for (int j = i * 7; j < (i + 1) * 7; j++) {
                    String name = "data";
                    switch (j % 7) {
                        case 0:
                            name = "序号";
                            break;
                        case 1:
                            name = "发生地震的时刻";
                            break;
                        case 2:
                            name = "经度";
                            break;
                        case 3:
                            name = "纬度";
                            break;
                        case 4:
                            name = "深度";
                            break;
                        case 5:
                            name = "震级";
                            break;
                        case 6:
                            name = "参考位置";
                            break;
                    }
                    single.put(name, data.get(j).text());
                }
                result.add(single);

            }
        }

        System.out.println(result);
        String s = result.toJSONString();
        return s;


    }
}
